package com.morgan.maxfunding;

import java.util.Calendar;

import com.morgan.submit.BusinessDetail;
import com.morgan.submit.CreditHistory;
import com.morgan.submit.Liabilities;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ScrollView;

public class NewSubmitActivity extends Activity implements View.OnClickListener {
	
	LinearLayout llytHeader;

	final static int VIEW_LIABILITIES 		= 3;
	final static int VIEW_DETAIL 			= 4;
	final static int VIEW_CREDIT_HISTORY 	= 5;
	
	LinearLayout 	llytLiabilities;
	LinearLayout 	llytDetail;
	LinearLayout 	llytCreditHistory;
	
	Button 			btnLiabilities;
	Button 			btnDetail;
	Button 			btnCreditHistory;
	
	Liabilities		viewLiabilities;
	BusinessDetail 	viewDetail;
	CreditHistory	viewCreditHistory;
	
	Button 			btnBack;
	Button 			btnSubmit;
	
	int				m_nSelectView;
	int 			nErrorCode;
	
	ScrollView		scrollView;
	
    @Override
	protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_new_submit);
        
        GlobalData.makeItemsOfRegisteredOwnersNameForMortorVehicle();
        GlobalData.makeItemsOfRegisteredOwnersNameForRealEstate();
        
        initWidget();
        initEvent();
        initValue();
    }
    
    private void initWidget() {
    	
		llytHeader			= (LinearLayout) findViewById(R.id.header_linearLayout);

    	scrollView			= (ScrollView) findViewById(R.id.scrollView1);

    	llytLiabilities 	= (LinearLayout) findViewById(R.id.liabilities_layout);
    	llytDetail 			= (LinearLayout) findViewById(R.id.detail_layout);
    	llytCreditHistory 	= (LinearLayout) findViewById(R.id.credit_history_layout);
    	
    	btnLiabilities 		= (Button) findViewById(R.id.liabilities_button);
    	btnDetail 			= (Button) findViewById(R.id.inf_under_finance_button1);
    	btnCreditHistory 	= (Button) findViewById(R.id.credit_history_button);
    	
    	btnBack				= (Button) findViewById(R.id.new_back_button);
    	btnSubmit			= (Button) findViewById(R.id.new_submit_button);
    	
		viewLiabilities		= new Liabilities(this);
		viewDetail  		= new BusinessDetail(this);
		viewCreditHistory	= new CreditHistory(this);
		
		llytLiabilities.addView(viewLiabilities.view);
		llytDetail.addView(viewDetail.view);
		llytCreditHistory.addView(viewCreditHistory.view);
    }
    
    private void savePreferences() {

    	viewLiabilities.savePreferences();
    	viewDetail.savePreferences();
    	viewCreditHistory.savePreferences();
    }
    
    private void selectView(int nView) {
    	
    	llytLiabilities.setVisibility(View.GONE);
    	llytDetail.setVisibility(View.GONE);
    	llytCreditHistory.setVisibility(View.GONE);
    	
    	savePreferences();
    	m_nSelectView = nView;
    	
    	if (m_nSelectView == VIEW_LIABILITIES) {
    		btnLiabilities.requestFocusFromTouch();
    		viewLiabilities.showMortGage();
    		llytLiabilities.setVisibility(View.VISIBLE);
    	}
    	if (m_nSelectView == VIEW_DETAIL) {
    		btnDetail.requestFocusFromTouch();

    		llytDetail.setVisibility(View.VISIBLE);
    		viewDetail.showNoneSelectedLayout();
    	}
    	if (m_nSelectView == VIEW_CREDIT_HISTORY) {
    		btnCreditHistory.requestFocusFromTouch();
    		llytCreditHistory.setVisibility(View.VISIBLE);
    	}
    }
    
    private void initEvent() {
    	
		llytHeader.setOnClickListener(new Button.OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://maxfunding.com.au"));
				startActivity(browserIntent);
			}
        });
		
    	btnBack.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View v) {
				finish();
			}
        });
//    	
    	btnSubmit.setOnClickListener(new Button.OnClickListener() {

			@Override
			public void onClick(View v) {
				savePreferences();
//				if (!viewAsset.checkInput()) {
//					selectView(VIEW_ASSET);
//					return;
//				}
				
//				if (!viewAsset.checkInput()) return;
//				if (!checkMotorVehicle()) return;
				if (!checkLiabilitiles()) return;
				
//				goCompletePage();
//				sendDataProc();
			}
        });
    	
    	btnLiabilities.setOnClickListener(this);
    	btnDetail.setOnClickListener(this);
    	btnCreditHistory.setOnClickListener(this);
    }
    
    private void initValue() {
    	m_nSelectView = VIEW_LIABILITIES;
    	selectView(m_nSelectView);
    }
    
    public boolean checkLiabilitiles() {
		nErrorCode = viewLiabilities.checkInput();
		if (nErrorCode != Liabilities.ERROR_NONE) {
			selectView(VIEW_LIABILITIES);
			
			if (nErrorCode == Liabilities.ERROR_BOARDING_RENTING_COST) {
				viewLiabilities.edtBoardingRentingCost.requestFocus();
			}
			if (nErrorCode == Liabilities.ERROR_OTHER_LIVING_EXPENSES) {
				viewLiabilities.edtOtherLivingExpenses.requestFocus();
			}
			if (nErrorCode == Liabilities.ERROR_OWING_CREDIT_LIMIT1) {
				viewLiabilities.edtOwingCreditLimet1.requestFocus();
			}
			if (nErrorCode == Liabilities.ERROR_MONTHLY_PAYMENT1) {
				viewLiabilities.edtMonthlyPayment1.requestFocus();
			}
			if (nErrorCode == Liabilities.ERROR_LENDER1) {
				viewLiabilities.edtLender1.requestFocus();
			}
			if (nErrorCode == Liabilities.ERROR_OWING_CREDIT_LIMIT2) {
				viewLiabilities.edtOwingCreditLimet2.requestFocus();
			}
			if (nErrorCode == Liabilities.ERROR_MONTHLY_PAYMENT2) {
				viewLiabilities.edtMonthlyPayment2.requestFocus();
			}
			if (nErrorCode == Liabilities.ERROR_LENDER2) {
				viewLiabilities.edtLender2.requestFocus();
			}
			if (nErrorCode == Liabilities.ERROR_BUSINESS_LOAN_OWING) {
				viewLiabilities.edtBusinessLoanOwing.requestFocus();
			}
			if (nErrorCode == Liabilities.ERROR_BUSINESS_LOAN_LENDER) {
				viewLiabilities.edtBusinessLoanLender.requestFocus();
			}
			if (nErrorCode == Liabilities.ERROR_CREDIT_CARD_OWING) {
				viewLiabilities.edtCreditCardOwing.requestFocus();
			}
			if (nErrorCode == Liabilities.ERROR_CREDIT_CARD_LENDER) {
				viewLiabilities.edtCreditCardLender.requestFocus();
			}
			return false;
		}
		
		return checkDetail();
    }
//    
//    public boolean checkPersonalLoan() {
//    	
//    	if (!m_isHasPersonalLoan)
//    		return checkDetail();
//    	
//		nErrorCode = viewLiabilities.checkPersonalLoanInput();
//		
//		if (nErrorCode != Liabilities.ERROR_NONE) {
//
//		}
//		return checkDetail();
//    }
    
    public boolean checkDetail() {
    	nErrorCode = viewDetail.checkInput();
		if (nErrorCode != BusinessDetail.ERROR_NONE) {
			selectView(VIEW_DETAIL);
			
			if (nErrorCode == BusinessDetail.ERROR_BUSINESS_NAME) {
				viewDetail.edtBusinessName.requestFocus();
			}
			if (nErrorCode == BusinessDetail.ERROR_NATURE_BUSINESS) {
				viewDetail.edtNatureBusiness.requestFocus();
			}
			if (nErrorCode == BusinessDetail.ERROR_DETAIL_ADDRESS) {
				viewDetail.edtDetailAddress.requestFocus();
			}
			if (nErrorCode == BusinessDetail.ERROR_NAME_DIRECTORS) {
				viewDetail.edtNameDirector.requestFocus();
			}
			if (nErrorCode == BusinessDetail.ERROR_MOST_LIKE_REPAY) {
				viewDetail.edtMostLikeRepay.requestFocus();
			}
			if (nErrorCode == BusinessDetail.ERROR_OTHER_SPECIFY) {
				viewDetail.edtOtherSpecify.requestFocus();
			}
			if (nErrorCode == BusinessDetail.ERROR_AVERAGE_CASH_SALES) {
				viewDetail.edtAverageCashSales.requestFocus();
			}
			if (nErrorCode == BusinessDetail.ERROR_AVERAGE_EFTPOS_SALES) {
				viewDetail.edtAverageEftPosSales.requestFocus();
			}
			if (nErrorCode == BusinessDetail.ERROR_STAGE_BUSINESS) {
				viewDetail.spnStageBusiness.requestFocusFromTouch();
			}
			if (nErrorCode == BusinessDetail.ERROR_TYPE_ENTITY) {
				viewDetail.spnTypeEntity.requestFocusFromTouch();
			}
			if (nErrorCode == BusinessDetail.ERROR_FIXED_END_MONTH) {
				viewDetail.spnFixedLeaseEndMonth.requestFocusFromTouch();
			}
			if (nErrorCode == BusinessDetail.ERROR_FIXED_END_YEAR) {
				viewDetail.spnFixedLeaseEndYear.requestFocusFromTouch();
			}
			if (nErrorCode == BusinessDetail.ERROR_STAGE_PROPERTY_SALE) {
				viewDetail.spnStagePropertySale.requestFocusFromTouch();
			}
			if (nErrorCode == BusinessDetail.ERROR_STAGE_REFINANCING) {
				viewDetail.spnStageRefinancing.requestFocusFromTouch();
			}
			
			return false;
		}
		
		return checkCreditHistory();
    }
    
    public boolean checkCreditHistory() {
    	nErrorCode = viewCreditHistory.checkInput();
    	if (nErrorCode != CreditHistory.ERROR_NONE) {
    		selectView(VIEW_CREDIT_HISTORY);
    		
    		if (nErrorCode == CreditHistory.ERROR_TOTAL_OUTSTANDING) {
    			viewCreditHistory.edtTotalOutStandingValue.requestFocus();
    		}

    		if (nErrorCode == CreditHistory.ERROR_HARD_SHIP) {
    			viewCreditHistory.btnNoApplyBankruptcy.requestFocusFromTouch();
    			viewCreditHistory.btnNoApplyBankruptcy.requestFocusFromTouch();
    		}
    		if (nErrorCode == CreditHistory.ERROR_CREDIT_HISTORY) {
    			viewCreditHistory.spnCreditHistory.requestFocusFromTouch();
    		}
    		if (nErrorCode == CreditHistory.ERROR_DISCHARGED_DATE) {
    			viewCreditHistory.spnDischargedDate.requestFocusFromTouch();
    		}
    		if (nErrorCode == CreditHistory.ERROR_DEFAULT_AFTER_DISCHARGE) {
    			viewCreditHistory.btnNoDefaultAfterDischarge.requestFocusFromTouch();
    			viewCreditHistory.btnYesDefaultAfterDischarge.requestFocusFromTouch();
    		}
    		if (nErrorCode == CreditHistory.ERROR_EXISTING_CLIENT) {
    			viewCreditHistory.btnNoTakenOutLoan.requestFocusFromTouch();
    			viewCreditHistory.btnYesTakenOutLoan.requestFocusFromTouch();
    		}
    		if (nErrorCode == CreditHistory.ERROR_CHECK_AGREE) {
    			viewCreditHistory.chkAgreeCredit.requestFocusFromTouch();
    		}

    		return false;
    	}
    	
		sendDataProc();
//		goCompletePage();
		
    	return true;
    }
    
    public void goNextPage() {
        
    	SharedPreferences sharedPreferences = HomeActivity.sharedPreferences;
    	SharedPreferences.Editor editor = sharedPreferences.edit();
    	
   	 	editor.putString("strSubmit", "submit");
   	 	
   	 	Calendar c = Calendar.getInstance();
		long lNowTime = c.getTimeInMillis();
   	 	editor.putLong("lastSubmissionTime", lNowTime);
		editor.putString("strSubmitResult", GlobalData.personalInfo.strDecision);
		editor.putString("appID", GlobalData.personalInfo.strAppID);
		editor.commit();
		
		MainActivity.saveExternalFileData();

    	if (GlobalData.personalInfo.countMotorVehicle == 0 && GlobalData.personalInfo.countRealState == 0) {
    		String strStageBusiness = GlobalData.personalInfo.strStageBusiness;
    		String strCreditHistory = GlobalData.personalInfo.strCreditHistory;
    		
    		int age = GlobalData.personalInfo.getAge();
    		int totalOutstanding = 0;
    		
    		if (!GlobalData.personalInfo.strTotalOutstanding.equals("")) {
    			totalOutstanding = Integer.parseInt(GlobalData.personalInfo.strTotalOutstanding);	
    		}
    		if ((strStageBusiness.equals(GlobalData.itemsOfStageBusiness[1]) || strStageBusiness.equals(GlobalData.itemsOfStageBusiness[3]) || strStageBusiness.equals(GlobalData.itemsOfStageBusiness[4]))) {
    			
    			startActivity(new Intent(this, UsbActivity2.class));
    			return;
    		} else /*if ((strCreditHistory.equals(GlobalData.itemsOfCreditHistory[1]) || (strCreditHistory.equals(GlobalData.itemsOfCreditHistory[2]))))*/ {
    			startActivity(new Intent(this, UsbActivity3.class));
    			return;
    		}
    	}
//		startActivity(new Intent(this, ErrorActivity.class));	
    	
//		startActivity(new Intent(this, CompleteActivity.class));	

    	if (GlobalData.personalInfo.strDecision.equals("Support Required")) {
    		startActivity(new Intent(this, DeclineActivity.class));
    	}
    	if (GlobalData.personalInfo.strDecision.equals("Pass")) {
    		startActivity(new Intent(this, CompleteActivity.class));	
    	}
    }
    
    public void showGeneralAlert(String title, String message) {
    	new AlertDialog.Builder(NewSubmitActivity.this)
        .setTitle(title)
        .setMessage(message)
        .setPositiveButton("OK", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
                // continue with delete
            }
         })
         .show();
    }
    
    public void sendDataProc() {

        try {
        	
//    		String android_id = Secure.getString(this.getContentResolver(), Secure.ANDROID_ID);
//    		
//    		GlobalData.strDeviceID = android_id;
//    		
//    		Log.e("Android","Android ID : " + android_id);
        	new MyAsyncTask(NewSubmitActivity.this, MyAsyncTask.ACTION_SIGNUP).execute("signup");
        	
        } catch (Exception e) {
        	
        }
    }
    
	public void onClick(View v) {
		if (v == btnLiabilities) {
			selectView(VIEW_LIABILITIES);
		}
		if (v == btnDetail) {
			selectView(VIEW_DETAIL);
		}
		if (v == btnCreditHistory) {
			selectView(VIEW_CREDIT_HISTORY);
//			viewCreditHistory.showBankruptyQuestion();
		}
	}
}
